package 力扣日常刷题.木22一月.第01天0108;

/**
 * @author 帅小伙
 * @date 2022/1/8
 * @description  有点模糊
 * https://leetcode-cn.com/problems/count-sub-islands/
 */
public class Demo05广度优先搜索统计子岛屿 {



        boolean isContain = true;
        public int countSubIslands(int[][] grid1, int[][] grid2) {
            int res = 0;
            for(int i = 0; i < grid2.length; i++){
                for(int j = 0; j < grid2[0].length; j++){
                    if(grid2[i][j] == 1){
                        dfs(grid1, grid2, i, j);
                        if(isContain){
                            res++;
                        }else{
                            isContain = true;
                        }
                    }
                }
            }
            return res;
        }

        public void dfs(int[][] grid1, int[][] grid2, int i, int j){
            if(i < 0 || i >= grid2.length || j < 0 || j >= grid2[0].length){
                return;
            }
            if(grid2[i][j] == 0){
                return;
            }
            if(grid1[i][j] != 1){
                isContain = false;
            }
            grid2[i][j] = 0;
            int[][] next = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
            for(int k = 0; k < 4; k++){
                dfs(grid1, grid2, i + next[k][0], j + next[k][1]);
            }
        }



}
